package de.wwu.ifgi.gischool.droidAR;

import geo.GeoObj;
import gl.Color;
import gl.GL1Renderer;
import gl.GLFactory;
import gl.scenegraph.Shape;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import system.ArActivity;
import system.DefaultARSetup;
import worldData.World;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;

public class MainActivity extends Activity {

	private ArrayList<ArrayList<ArrayList<Double>>> polygons;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		InputStream is = getResources().openRawResource(R.raw.graefte);
		Writer writer = new StringWriter();
		char[] buffer = new char[1024];
		try {
			Reader reader = new BufferedReader(new InputStreamReader(is,
					"UTF-8"));
			int n;
			while ((n = reader.read(buffer)) != -1) {
				writer.write(buffer, 0, n);
			}
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				is.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		String jsonString = writer.toString();
		Log.i("JSON", jsonString);

		polygons = new ArrayList<ArrayList<ArrayList<Double>>>();
		try {

			JSONObject jsonObject = new JSONObject(jsonString);
			JSONArray features = jsonObject.getJSONArray("features");
			for (int i = 0; i < features.length(); i++) {
				JSONObject feature = features.getJSONObject(i);
				JSONObject geometry = feature.getJSONObject("geometry");
				JSONArray coordinates = geometry.getJSONArray("coordinates");
				ArrayList<ArrayList<Double>> polygon = new ArrayList<ArrayList<Double>>();
				for (int j = 0; j < coordinates.length(); j++) {
					JSONArray latlong = coordinates.getJSONArray(j);
					double latitude = latlong.getDouble(1);
					double longitude = latlong.getDouble(0);
					ArrayList<Double> point = new ArrayList<Double>();
					point.add(latitude);
					point.add(longitude);
					polygon.add(point);
				}
				polygons.add(polygon);
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		for (ArrayList<ArrayList<Double>> polygon : polygons) {
			Log.i("Polygon", "Neues Polygon:");
			for (ArrayList<Double> point : polygon) {
				Log.i("Point", point.get(0) + "," + point.get(1));
			}
		}
		ArActivity.startWithSetup(MainActivity.this, new DefaultARSetup() {

			@Override
			public void addObjectsTo(GL1Renderer renderer, World world,
					GLFactory objectFactory) {
				// TODO Auto-generated method stub
				for (ArrayList<ArrayList<Double>> polygon : polygons) {
					Log.i("Polygon", "Neues Polygon:");
					for (int k = 0; k < polygon.size(); k++) {
						ArrayList<Double> point1 = null;
						ArrayList<Double> point2 = null;
						if (k < polygon.size() - 1) {
							point1 = polygon.get(k);
							point2 = polygon.get(k + 1);
						} else {
							point1 = polygon.get(k);
							point2 = polygon.get(0);
						}
						Log.i("Point", point1.get(0) + "," + point1.get(1));
						GeoObj p1 = new GeoObj(point1.get(0), point1.get(1));
						GeoObj p2 = new GeoObj(point2.get(0), point2.get(1));
						Shape shape = objectFactory.newDirectedPath(p1, p2,
								Color.red());
						shape.setPosition(p1.getVirtualPosition());
						world.add(shape);

					}
				}

			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
}
